package com.cennavi.minenavidemo.activity.basic;

import android.graphics.Bitmap;
import android.graphics.Point;
import android.os.Bundle;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;

import com.cennavi.minenavidemo.R;
import com.cennavi.minenavidemo.presenter.MapPresenter;
import com.cennavi.minenavidemo.widget.MyCheckButton;
import com.cennavi.minenavidemo.widget.MyMapView;
import com.google.android.material.snackbar.Snackbar;
import com.minedata.minenavi.map.CompassView;
import com.minedata.minenavi.map.MineMap;
import com.minedata.minenavi.map.ScaleView;
import com.minedata.minenavi.map.TrafficEventInfo;
import com.minedata.minenavi.map.ZoomView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import butterknife.Unbinder;

import static android.view.View.GONE;
import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;

public class BaseMapActivity extends AppCompatActivity implements MapPresenter.OnMapViewListener,
        MyMapView.Listener {

    private MineMap mMineMap;
    private MapPresenter mMapPresenter;

    private Unbinder mUnbinder;
    @BindView(R.id.mapView)
    MyMapView mMapView;
    @BindView(R.id.compassView)
    CompassView mCompassView;
    @BindView(R.id.zoomView)
    ZoomView mZoomView;
    @BindView(R.id.scaleView)
    ScaleView mScaleView;
    @BindView(R.id.ivLocation)
    ImageView mLocationImageView;
    @BindView(R.id.btn_tmc)
    MyCheckButton mTmcBtn;
    @BindView(R.id.btn_traffic_event)
    MyCheckButton mTrafficEventBtn;
    @BindView(R.id.btn_screen_shot)
    Button mScreenShotBtn;
    @BindView(R.id.layout_screen_shot)
    FrameLayout mScreenShotLayout;
    @BindView(R.id.iv_screen_shot)
    ImageView mScreenShotImageView;
    @BindView(R.id.btn_pan)
    Button mPanBtn;
    @BindView(R.id.btn_map_bound)
    Button mMapBoundBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basemap);
        mUnbinder = ButterKnife.bind(this);
        init();
        initView();
    }

    public void init() {
        mMapPresenter = MapPresenter.getInstance();
        mMapPresenter.init(BaseMapActivity.this, mMapView);
        mMapPresenter.setOnMapViewListener(this);
    }

    public void initView() {
        mCompassView.setMapView(mMapView);

        mZoomView.setMapView(mMapView);
        String[] zoomOutFileNames = new String[]{
                "navicore/res/zoomButton/zoom_out_normal.png",
                "navicore/res/zoomButton/zoom_out_disable.png",
                "navicore/res/zoomButton/zoom_out_pressed.png"};
        String[] zoomInFileNames = new String[]{
                "navicore/res/zoomButton/zoom_in_normal.png",
                "navicore/res/zoomButton/zoom_in_disable.png",
                "navicore/res/zoomButton/zoom_in_pressed.png"};
        int[] types = new int[]{ZoomView.Type.normal, ZoomView.Type.disabled,
                ZoomView.Type.pressed};
        mZoomView.setZoomOutBackground(zoomOutFileNames, types);
        mZoomView.setZoomInBackground(zoomInFileNames, types);

        mScaleView.setMapView(mMapView);
        mScaleView.setDrawPos(ScaleView.Pos.leftBottom);

        mTmcBtn.enableChecked(true);
        mTrafficEventBtn.enableChecked(true);
    }

    @OnClick(R.id.ivLocation)
    public void switchLocation() {
        mMapPresenter.switchLocation();
    }

    @OnClick(R.id.btn_tmc)
    public void tmc() {
        //路况开关
        mMapPresenter.getMineMap().enableTmc(!mMapPresenter.getMineMap().isTmcEnabled());

        if (mMapPresenter.getMineMap().isTmcEnabled()) {
            mTmcBtn.enableChecked(true);
        } else {
            mTmcBtn.enableChecked(false);
        }
    }

    @OnClick(R.id.btn_traffic_event)
    public void traffic() {
        //交通事件开关
        mMapPresenter.getMineMap().enableTrafficEventLayer(!mMapPresenter.getMineMap().isTrafficEventLayerEnable());

        if (mMapPresenter.getMineMap().isTrafficEventLayerEnable()) {
            mTrafficEventBtn.enableChecked(true);
        } else {
            mTrafficEventBtn.enableChecked(false);
        }
    }

    @OnCheckedChanged({R.id.rb_2d, R.id.rb_3d, R.id.rb_satellite})
    public void switchLayer(RadioButton radioButton) {
        boolean checked = radioButton.isChecked();

        switch (radioButton.getId()) {
            case R.id.rb_2d:
                if (checked) {
                    mMapPresenter.switch2D();
                }

                break;
            case R.id.rb_3d:
                if (checked) {
                    mMapPresenter.switch3D();
                }

                break;
            case R.id.rb_satellite:
                if (checked) {
                    mMapPresenter.switchSatelliteLayer();
                }

                break;
        }
    }

    @OnClick(R.id.btn_screen_shot)
    public void screenshot() {
        mScreenShotLayout.setVisibility(VISIBLE);
        Bitmap bitmap = mMapView.screenShot();
        mScreenShotImageView.setImageBitmap(bitmap);
    }

    @OnClick(R.id.layout_screen_shot)
    public void lscreenshot() {
        mScreenShotLayout.setVisibility(GONE);
    }

    @OnClick(R.id.btn_map_bound)
    public void mapbound() {
        mMapPresenter.mapbound();
    }

    @OnClick(R.id.btn_pan)
    public void pan() {
        mMapPresenter.getMineMap().pan(new Point(40, 0));
    }

    @Override
    protected void onPause() {
        if (mMapView != null) {
            mMapView.onPause();
        }

        super.onPause();
    }

    @Override
    protected void onResume() {
        if (mMapView != null) {
            mMapView.onResume();
        }

        super.onResume();
    }

    @Override
    protected void onDestroy() {
        mMapPresenter.deleteMyLocationStyle();

        if (mMapView != null) {
            mMapView.onDestroy();
        }

        mUnbinder.unbind();
        super.onDestroy();
    }

    @Override
    public void onScrollStarted() {

    }

    @Override
    public void onScrollFinished() {

    }

    @Override
    public void onCameraChanged(int changeType) {
        //控制指北针的显示
        if ((changeType & MineMap.CameraSetting.heading) == MineMap.CameraSetting.heading) {
            mCompassView.updateHeading(mMapPresenter.getMineMap().getHeading());
        }
        if (mMapPresenter.getMineMap().getElevation() == 90.f) {
            if (mCompassView.getVisibility() == VISIBLE && mMapPresenter.getMineMap().getHeading() == 0.f) {
                mCompassView.setVisibility(GONE);
            }
        } else {
            if (mCompassView.getVisibility() == INVISIBLE) {
                mCompassView.setVisibility(VISIBLE);
            }
        }
        //控制比例尺的显示
        mScaleView.updateScale(mMapPresenter.getMineMap().getZoomLevel());
    }

    @Override
    public void onTrafficEventIconClicked(int type, TrafficEventInfo trafficEventInfo) {
        //点击交通事件icon的事件详情显示
        Snackbar.make(mMapView, trafficEventInfo.type + "\n" + trafficEventInfo.description, Snackbar.LENGTH_SHORT).show();
    }

    @Override
    public void onMapReady() {
        mMineMap = mMapPresenter.getMineMap();
        mMineMap.enableTrafficEventLayer(true);
        mMineMap.enableTmc(true);

        //mMineMap.setStyleClass("BLUE");
        //mMineMap.setStyleClass("BLUE,blue-ti");
        //mMineMap.setStyleClass("NAVIGATION-BLUE");
        // 设置地图中心点
//            Point point = new Point(12354878, 4183434);
//            mMineMap.setPointToCenter(point.x, point.y);

        mMapPresenter.addMyLocationStyle();
        mMapView.addListener(this);
    }

    @Override
    public void onswitchLocation(int imageLevel) {
        mLocationImageView.setImageLevel(imageLevel);
    }
}
